/*
 * Copyright 2011 Michael Guo
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uc.cs.guome.ir;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;

/**
 * The object containing the list of stop words
 * @author(name="Michael Guo",date="01/18/2011")
 * @version 0.1
 * @see java.io.StreamTokenizer
 */
public class StopWordList implements Serializable {
	
	private static final long serialVersionUID = 5475473029363004309L;
	private HashSet<String> stopWordSet;
	
	public StopWordList(String fname) {
		setStopWordSet(new HashSet<String>());
		try{
			FileInputStream fis = new FileInputStream(fname);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			
			String line = null;
			while((line= br.readLine())!=null) {
				this.stopWordSet.add(line);
			}
			dis.close();
		}catch(FileNotFoundException e) {
			System.out.println("Stop word file " + fname + " not found!");
			return;
		} catch (IOException e) {
			System.out.println("Stop word file " + fname + " reading error!");
			return;
		}
	}
	
	public void saveStopWordSet(String swfname) {
		
		if (this.stopWordSet == null) {
			System.out.println("Stop words not exist!");
			return;
		}
		try {
			PrintWriter pw = new PrintWriter(new FileWriter(swfname));
			Iterator<String> ir = this.stopWordSet.iterator();
			while(ir.hasNext())
				pw.println(ir.next());
			pw.close();
		}catch(IOException e) {
			System.out.println("Stop words save error");
			return;
		}
	}

	public void setStopWordSet(HashSet<String> stopWordSet) {
		this.stopWordSet = stopWordSet;
	}

	public HashSet<String> getStopWordSet() {
		return stopWordSet;
	}
	
	public boolean isStopWord(String sw) {
		return (this.stopWordSet.contains(sw));
	}
}
